package view.rules;

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import components.Children;
import components.Component;

import ontology.Concept;
import ontology.ConceptList;

import features.Feature;
import features.FeatureList;
import ruletree.rule.Action;
import ruletree.rule.Rule;
import ruletree.rule.Word;

import features.Feature;
import features.FeatureList;

import lexicon.LexiconList;
import managers.OntologyManager;

import view.rules.TriggerPanel;

import net.miginfocom.swing.MigLayout;

public class SpelledOutPanelA extends JPanel{
        
        TriggerPanel triggerPanel;
        SubmitPanel submitPanel;
        FeaturePane featurePane;
        private JPanel secondPanel;
        private JPanel thirdPanel;
        private JButton structureButton;
        private JLabel baseFormLabel;
        private JComboBox<String> baseFormCombo;
        private JButton featuresButton;
        private JLabel prefixLabel;
        private JTextField prefixTextField;
        private JLabel suffixLabel;
        private JTextField suffixTextField;
        private JLabel glossLabel;
        private JTextField glossTextField;
        private JLabel affixTagLabel;
        private JComboBox prefixTagCombo;
        private JLabel commentLabel;
        private JTextArea commentArea;
        private JPanel affixPanel;
        private JPanel glossPanel;
          	
    	boolean prefix;
    	boolean suffix;
    	boolean infix;
    	boolean circum;
    	boolean newWord;
    	boolean addWord;

        TriggerButtonListener triggerListener;
        FeatureButtonListener featureButtonListener;
        private LexiconList list;
        private ArrayList<Feature> featureList;
        private String posFeatures;
        private String ruleName;
        
        SubmitOkButtonListener submitOkButtonListener;

        
        public SpelledOutPanelA(String posFeatures){
            this.setLayout(new MigLayout());
            this.posFeatures = posFeatures;
            initComponents();       
            addComponents();
        }
		public SpelledOutPanelA(){
                this.setLayout(new MigLayout());
                initComponents();       
                addComponents();
        }
        
        private void initComponents(){
                
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            
            triggerPanel = new TriggerPanel();
            
            structureButton = new JButton("Structure");
            
            secondPanel = new JPanel();
            secondPanel.setLayout(new MigLayout());
            baseFormLabel = new JLabel("Base Form:");
            baseFormCombo = new JComboBox<String>();
           
            baseFormCombo.setMinimumSize(new Dimension(200, 20));
            featuresButton = new JButton("Features");

            featuresButton.addActionListener(new ActionListener() {
            	public void actionPerformed(ActionEvent e) {
            		//System.out.println(posFeatures + "(((((()))))))))");
            		featureButtonListener.setPOS(posFeatures);
					featurePane.setTextArea(featureButtonListener.getFeatures());
				
            	}
            });

        	featureButtonListener = new FeatureButtonListener();
            featuresButton.addActionListener(featureButtonListener);

            featurePane = new FeaturePane();
            featurePane.setMinimumSize(new Dimension((int)screenSize.getWidth() - 500, 80));
            
            thirdPanel = new JPanel();
            thirdPanel.setLayout(new MigLayout());
            affixPanel = new JPanel();
            affixPanel.setLayout(new MigLayout());
            prefixLabel = new JLabel("Prefix:");
            prefixTextField = new JTextField(40);
            suffixLabel = new JLabel("Suffix");
            suffixTextField = new JTextField(40);
            affixPanel.add(prefixLabel);
            affixPanel.add(prefixTextField);
            glossPanel = new JPanel();
            glossPanel.setLayout(new MigLayout());
            glossLabel = new JLabel("Gloss:");
            glossTextField = new JTextField(40);
            glossPanel.add(glossLabel);
            glossPanel.add(glossTextField);
            affixTagLabel = new JLabel("Prefix's Tag:");
            prefixTagCombo = new JComboBox();
            prefixTagCombo.setMinimumSize(new Dimension(200, 20));
            
            commentLabel = new JLabel("Comment:");
            commentArea = new JTextArea();
            commentArea.setMinimumSize(new Dimension((int)screenSize.getWidth() - 200, (int)screenSize.getHeight() - 600));
                
            submitPanel = new SubmitPanel();
            submitOkButtonListener = new SubmitOkButtonListener();
            submitPanel.okButton.addActionListener(submitOkButtonListener);
            
            triggerListener = new TriggerButtonListener();
            triggerPanel.triggerButton.addActionListener(triggerListener);
            		
    		prefix = true;
    		suffix = false;
    		infix = false;
    		circum = false;
    		newWord = false;
    		addWord = false;
        }
        
        private void addComponents(){
                
                add(triggerPanel, "wrap");
                addToSecondPanel();
                add(secondPanel, "wrap");
                addToThirdPanel();
                add(thirdPanel, "wrap");
                add(commentLabel, "wrap");
                add(commentArea, "wrap");
                add(submitPanel);
                          
        }
        
        private void addToSecondPanel(){
                secondPanel.add(baseFormLabel);
                secondPanel.add(baseFormCombo, "wrap");
                secondPanel.add(featuresButton);
                secondPanel.add(featurePane);
        }
        
        private void addToThirdPanel(){
                
                thirdPanel.add(affixPanel);
                thirdPanel.add(affixTagLabel, "gapleft 100");
                thirdPanel.add(prefixTagCombo, "wrap");
                thirdPanel.add(glossPanel);
                
        }
        
        public void setFormModel(LexiconList Lexlist){
    		DefaultComboBoxModel<String> model = new DefaultComboBoxModel();
    		ArrayList<String> list = Lexlist.getPossibleForms();
    
    		for(String p: list){
    			model.addElement(p);
    		}
    		baseFormCombo.setModel(model);
    		//return model;  		
    	}
        
        public void setTriggerWordModel(String pos){
    		DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
    		ConceptList conceptList = OntologyManager.getInstance().getConceptList(pos);
    		ArrayList<Concept> list = conceptList.getConceptList();
    		for(Concept p: list){
    			model.addElement(p.getName());
    		}
    		triggerPanel.setModel(model);
    		//return model;
    	}

		public LexiconList getList() {
			return list;
		}
		public void setList(LexiconList list) {
			this.list = list;
	
		}
		
		public String getPosFeatures() {
			return posFeatures;
		}
		public void setPosFeatures(String posFeatures, String ruleName) {
			this.posFeatures = posFeatures;
			this.ruleName = ruleName;
			//System.out.println(ruleName);
		}
		
		public void saveRule(){
            JComboBox triggerCombo = triggerPanel.getComboBox();
            
            Children child = new Children(new ArrayList<Component>());
            FeatureList featureList = new FeatureList(new ArrayList<Feature>());
            ArrayList<Action> actionList = new ArrayList<>();
            Map<String, ArrayList<String>> selectedFeatures  = new HashMap<String, ArrayList<String>>();
            String var = "$1";
            String actionName = "addTranslation";
            
            String affix = "";
            
            Rule rule = new Rule(ruleName);
    
            String TriggerWord = (String) triggerCombo.getSelectedItem();
            Component component = Component.createInstance(TriggerWord, true);
            component.setVar(var);
            
            child.addChild(component);
            
            selectedFeatures = featurePane.getFeatures();
          
             Iterator it = selectedFeatures.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry pairs = (Map.Entry)it.next();
                                        
                    ArrayList<String> featureValues= (ArrayList<String>)pairs.getValue();  
               
                    for(int i = 0; i < featureValues.size(); i ++){
                            Feature feature = new Feature(pairs.getKey().toString(), featureValues.get(i), true);
                            //System.out.println("Feature Values are " + pairs.getKey().toString() + ", "+ featureValues.get(i));
                            featureList.setFeature(feature);
                    }
                   
                }
                
             Children actionChildren  = new Children(new ArrayList<Component>());   
             Component actionComponent = Component.createInstance(TriggerWord, true);  
             Word word = new Word(TriggerWord);
             
             if(prefix){
            	 affix = prefixTextField.getText();
            	 word.setPrefix(affix);
             }
             else if(suffix){
            	 affix = suffixTextField.getText();
                 word.setSuffix(affix); 
             }
           
             actionComponent.setVar(var);
             //actionComponent.setWord(word);
             actionChildren.addChild(actionComponent);
             
             Action action = new Action(actionName);
             action.setVar(var);
             action.setFeatureList(featureList);
             action.setChildren(actionChildren);      
                
            actionList.add(action);
             
            rule.setChildrenList(child);
            rule.setActionList(actionList);
            File f = new File("Languages\\Filipino\\XMLRules\\" + ruleName +".xml");
            rule.saveRule(f);
		}
		
		public void setComponent(String type){
			
			if (type.equals("prefix") && prefix == false){
				
				if (suffix == true || circum == true){
					affixPanel.remove(suffixLabel);
					affixPanel.remove(suffixTextField);
				}

				if (infix = true || newWord == true || addWord == true)
					prefixLabel.setText("Prefix:");
				
				affixPanel.add(prefixLabel);
				affixPanel.add(prefixTextField);
				affixTagLabel.setText("Prefix's Tag:");
				affixTagLabel.setVisible(true);
				prefixTagCombo.setVisible(true);
				baseFormLabel.setVisible(true);
				baseFormCombo.setVisible(true);
				
				prefix = true;
				suffix = false;
				infix = false;
				circum = false;
				newWord = false;
				addWord = false;
			}
			
			if (type.equals("suffix") && suffix == false){
				
				if (prefix == true || circum == true || newWord == true || addWord == true){
					affixPanel.remove(prefixLabel);
					affixPanel.remove(prefixTextField);
				} else if (infix == true){
					affixPanel.remove(prefixLabel);
					affixPanel.remove(prefixTextField);
				}
				
				affixPanel.add(suffixLabel);
				affixPanel.add(suffixTextField);
				affixTagLabel.setText("Suffix's Tag:");
				affixTagLabel.setVisible(true);
				prefixTagCombo.setVisible(true);
				baseFormLabel.setVisible(true);
				baseFormCombo.setVisible(true);
				
				prefix = false;
				suffix = true;
				infix = false;
				circum = false;
				newWord = false;
				addWord = false;
			}
			
			if (type.equals("infix") && infix == false){
				prefixLabel.setText("Infix:");
				affixTagLabel.setText("Infix's Tag:");
				
				if (suffix == true || circum == true){
					affixPanel.remove(suffixLabel);
					affixPanel.remove(suffixTextField);
					affixPanel.add(prefixLabel);
					affixPanel.add(prefixTextField);
				}
				
				affixTagLabel.setVisible(true);
				prefixTagCombo.setVisible(true);
				baseFormLabel.setVisible(true);
				baseFormCombo.setVisible(true);
				
				prefix = false;
				suffix = false;
				infix = true;
				circum = false;
				newWord = false;
				addWord = false;
				
			}
			
			if (type.equals("circum") && circum == false){
				if (prefix == true || infix == true || addWord == true){
					affixPanel.remove(prefixLabel);
					affixPanel.remove(prefixTextField);
					
				}
				if (suffix == true){
					affixPanel.remove(suffixLabel);
					affixPanel.remove(suffixTextField);
				}

				prefixLabel.setText("Prefix:");
				affixPanel.add(prefixLabel);
				affixPanel.add(prefixTextField);
				affixPanel.add(suffixLabel, "gapleft 20");
				affixPanel.add(suffixTextField);
				affixTagLabel.setText("Cicumfix's Tag:");
				affixTagLabel.setVisible(true);
				prefixTagCombo.setVisible(true);
				baseFormLabel.setVisible(true);
				baseFormCombo.setVisible(true);
				
				prefix = false;
				suffix = false;
				infix  = false;
				circum = true;
				newWord = false;
				addWord = false;
			}
			
			if (type.equals("newWord") && newWord == false){
				if (prefix == true || infix == true || circum == true){
					prefixLabel.setText("Translation:");
				}
				
				if (suffix == true || circum == true){
					affixPanel.remove(suffixLabel);
					affixPanel.remove(suffixTextField);
					affixPanel.add(prefixLabel);
					affixPanel.add(prefixTextField);
				}
				
				affixTagLabel.setVisible(false);
				prefixTagCombo.setVisible(false);
				baseFormLabel.setVisible(false);
				baseFormCombo.setVisible(false);
				
				prefix = false;
				suffix = false;
				infix = false;
				circum = false;
				newWord = true;
				addWord = false;
				
			}
			
			if (type.equals("addWord") && addWord == false){
				
				prefixLabel.setText("New Wrod:");
				affixTagLabel.setText("Word's Tag:");
				
				if (suffix == true){
					affixPanel.remove(suffixLabel);
					affixPanel.remove(suffixTextField);
					affixPanel.add(prefixLabel);
					affixPanel.add(prefixTextField);
				}
				
				if (circum == true){
					affixPanel.remove(suffixLabel);
					affixPanel.remove(suffixTextField);				
				}
				
				affixTagLabel.setVisible(true);
				prefixTagCombo.setVisible(true);
				baseFormLabel.setVisible(true);
				baseFormCombo.setVisible(true);
				
				prefix = false;
				suffix = false;
				infix = false;
				circum = false;
				newWord = false;
				addWord = true;
			}
		}
		
		public class SubmitOkButtonListener implements ActionListener{

            @Override
            public void actionPerformed(ActionEvent e) {
                
            	if (e.getSource() == submitPanel.okButton)
                   saveRule();
                    
            }
            
		}
}